package com.kevin.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFNum;
import org.apache.poi.xwpf.usermodel.XWPFNumbering;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;

/**
 * kevin<br/>
 * 2022/8/26 10:16<br/>
 */
public class DocxUtil {

    public static void main(String[] args) throws IOException {
        String filepath = "C:\\Users\\king\\Desktop\\策划-2016.docx";
        parseWordWithNumbering(filepath);
        System.out.println("=================");
    }

    public static void parseWordWithNumbering(String filepath) throws IOException {
        try (InputStream is = new FileInputStream(filepath);) {
            if (filepath.endsWith(".doc")) {
                WordExtractor wordExtractor = new WordExtractor(is);
                String content = wordExtractor.getText().trim();
                System.out.println(content);
            } else if (filepath.endsWith(".docx")) {
                XWPFDocument doc = new XWPFDocument(is);
                XWPFNumbering numbering = doc.getNumbering();
                for (XWPFParagraph para : doc.getParagraphs()) {
                    // 获取段落所属的编号ID
                    BigInteger numID = para.getNumID();
                    if (numID != null) {
                        XWPFNum num = numbering.getNum(numID);
                        if (num != null) {
                            System.out.println(para.getStyle() + "--" + para.getNumFmt() + "**" + para.getText());
                        }
                    } else {
                        System.out.println(para.getText());
                    }
                }
            }
        }
    }

    public static void parseWord(String filepath) throws IOException {
        try (InputStream is = new FileInputStream(filepath);) {
            if (filepath.endsWith(".doc")) {
                WordExtractor wordExtractor = new WordExtractor(is);
                String content = wordExtractor.getText().trim();
                System.out.println(content);
                System.out.println("-------------------------------");
                String[] paragraphs = content.split("\n");
                System.out.println(paragraphs.length);
                for (int i = 0; i < paragraphs.length; i++) {
                    System.out.println(i + "=" + paragraphs[i]);
                }
            } else if (filepath.endsWith(".docx")) {
                XWPFDocument xwpfDocument = new XWPFDocument(is);
                XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(xwpfDocument);
                String content = xwpfWordExtractor.getText().trim();
                System.out.println(content);
                System.out.println("-------------------------------");
                String[] paragraphs = content.split("\n");
                System.out.println(paragraphs.length);
                for (int i = 0; i < paragraphs.length; i++) {
                    System.out.println(i + "=" + paragraphs[i]);
                    System.out.println(StringUtils.isEmpty(paragraphs[i]));
                }
            }
        }
    }
}
